#include "mg_file.h"
#include "stdlib.h"
#include "mg_crc.h"

FILE_TRANSFER_CACHE Photo_File[3];

//文件长度
long file_size(FILE * stream)
{
	long curpos, length;
	curpos = ftell(stream);
	fseek(stream, 0L, SEEK_END);
	length = ftell(stream);
	fseek(stream, curpos, SEEK_SET);

	return length;
}
//检测文件是否存在
bool is_file_exist(char *path)
{
	FILE *fp = fopen(path, "rb");
	if (fp == NULL)
		return false;
	int len = file_size(fp);
	if (len == 0)
		return false;
	return true;
}
//获取数据
int file_get(char *path, int beg, char *buf, int size)
{
	if (path == NULL || buf == NULL || size == 0) {
		return -1;
	}
	FILE *fp = fopen(path, "rb");
	if (fp == NULL) {
		return -2;
	}
	int ret = 0, ok = 0;
	int len = file_size(fp);
	if (len == 0) {
		ok = -3; goto end;
	}
	if (beg > len) {
		ok = -4; goto end;
	}
	ret = fseek(fp, beg, SEEK_SET);
	if (ret != 0) {
		ok = -5; goto end;
	}
	size = (((size) < (len - beg)) ? (size) : (len - beg));
	len = fread(buf, size, 1, fp);
	if (len == 0) {
		ok = -6; goto end;
	}
	ok = size;

end:
	fclose(fp);

	return ok;
}
//保存数据
int file_set(char *path, int beg, char *buf, int size)
{
	if (path == NULL || buf == NULL || size == 0) {
		return -1;
	}
	FILE *fp = NULL;
	if (beg < 0) {   //清0文件并创建
		fp = fopen(path, "w+");
		beg = 0;
	}
	else {            //打开存在文件
		fp = fopen(path, "rb+");
		printf("Moorgen INFO : open file %s\r\n", path);
	}
	if (fp == NULL) { //创建文件
		fp = fopen(path, "wb+");
		printf("Moorgen INFO : The file does not exist, create file %s\r\n", path);
	}
	if (fp == NULL) {
		printf("Moorgen INFO : create file %s fail!\r\n", path);
		return -2;
	}
	printf("Moorgen INFO : create file %s success!\r\n", path);
	int ret, len, ok = 0;
	ret = fseek(fp, beg, SEEK_SET);
	if (ret != 0) {
		ok = -3; goto end;
	}
	len = fwrite(buf, size, 1, fp);
	if (len == 0) {
		ok = -4; goto end;
	}
	
end:
	fclose(fp);

	return ok;
}
//读文件
int32_t sf_read(uint8_t folder, uint16_t name, uint32_t ofs, uint8_t *buf, uint32_t *size)
{
	char path[64];
	int32_t ret = -1;

	if (folder == 0 || name == 0 || buf == NULL || size == NULL) {
		return -1;
	}
	sprintf(path, "%s" PICTURE_FILE_NAME, PICTURE_ROOT, folder, name);

	ret = file_get(path, ofs, buf, *size);
	if (ret >= 0) { *size = ret;}
	
	printf("path = %s, ret = %d\r\n", path, ret);
	
	return (ret < 0) ? ret : FR_OK;
}
//写文件
int32_t sf_write(uint8_t folder, uint16_t name, int32_t ofs, uint8_t *buf, uint32_t size)
{
	char path[64];
	int32_t ret = -1;
	
	if (folder == 0 || name == 0 || ((ofs < 0) ? 0 : ofs) + size > PICTURE_STORAGE_SPACE) {
		return -1;
	}
	sprintf(path, "%s" PICTURE_FILE_NAME, PICTURE_PATH, folder, name);
	
	ret = file_set(path, ofs, buf, size);

	printf("path = %s, size = %d, ret = %d\r\n", path, size, ret);
	
	return (ret < 0) ? ret : FR_OK;
}
//删除文件
int32_t sf_del(char *path)
{
	//remove(path);
	return FR_OK;
}
//文件初始化
void file_init(void)
{
}

//PNG图片保存
void Image_file_write(uint8_t number, FILE_TRANSFER_CACHE *pFile)
{
	char path[64];
	int32_t ret = -1,i;
	if (number == 0 || number > PICTURE_MAX_NUM)return;
	//
	memcpy(&Photo_File[number], pFile, sizeof(FILE_TRANSFER_CACHE));
	//对应地址写入PNG图片文件
	WritePhotoData(number);
	/*sprintf(path, "%sPhoto%d.png", PICTURE_PATH, number);
	ret = file_set(path, 0, file_buf, size);
	printf("Moorgen INFO : write file path = %s, size = %d, ret = %d\r\n", path, size, ret);*/

	////读取对应地址PNG图片文件
	//uint8_t read_data[PICTURE_MAX_SIZE];
	//uint32_t read_size = size;
	//memset(read_data, 0, sizeof(read_data));
	//ret = file_get(path, 0, read_data, read_size);
	//printf("Moorgen INFO : read file path = %s, ret = %d, read size = %d\r\n", path, ret, read_size);
	//
	//if (memcmp(read_data, file_buf, size))
	//	printf("Moorgen INFO : The read data is different from the written data\r\n");
	//else
	//	printf("Moorgen INFO : The read data is same as the written data\r\n");
	
	return;
}